package com.jxpanda.commons.encrypt;

import com.jxpanda.commons.constant.StringConstant;
import com.jxpanda.commons.toolkit.Base64Kit;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.util.function.Consumer;

/**
 * @author Panda
 */
@SuppressWarnings("AlibabaClassNamingShouldBeCamel")
public class AES {

    public static class ECB extends AbstractEncrypt.Symmetric {

        private static final int KEY_MAX_LENGTH = 16;

        public ECB(byte[] keyBytes) {
            super(EncryptAlgorithm.AES, keyBytes);
        }

        public ECB(String key) {
            super(EncryptAlgorithm.AES, key);
        }

        public ECB(Key key) {
            super(EncryptAlgorithm.AES, key);
        }


        @Override
        protected Key covert2Key(String key) {
            if (key.length() < KEY_MAX_LENGTH) {
                var need = KEY_MAX_LENGTH - key.length();
                key = key + StringConstant.ID_DEFAULT.repeat(need - 1);
            }
            return super.covert2Key(key.substring(0, 16));
        }
    }

    @Slf4j
    public static class CBC extends AbstractEncrypt.Symmetric {

        public CBC(byte[] keyBytes, byte[] ivBytes) {
            super(EncryptAlgorithm.AES_CBC_PKCS7);
            this.key = new SecretKeySpec(keyBytes, algorithm.getValue());
            this.spec = new IvParameterSpec(ivBytes);
        }

        public CBC(String key, String iv) {
            super(EncryptAlgorithm.AES_CBC_PKCS7);
            this.key = new SecretKeySpec(key.getBytes(), algorithm.getValue());
            this.spec = new IvParameterSpec(iv.getBytes());
        }

        @Override
        public String decrypt(String content) {
            return new String(super.decrypt(Base64Kit.decode(content)), StandardCharsets.UTF_8);
        }
    }

    @SneakyThrows
    public static void main(String[] args) {
        var appId = "wx4f4bc4dec97d474b";
//        var sessionKey = "DzQEtV5vTa8Yb3+2nFufsg==";
        var sessionKey = "2fb5/Q48swnSx5gu/H8kUw==";
//        var encryptedData = "PZxz/jfMs1q3+QUqkPznqgEJLHNhv2KM/99XOHYOmXK6dpjzP3ukTE7b8OPKcDe4Ula6zA5J+iJqng7+CfhmuVNG8AWQuAyvU9PtaC+jlFglSpV4VLX/ukovqel8DDyc0JKLpIDAEir8llABd32Rbr+O9Cih4T93tbKFPp3G/h0+FVS23JBYbDHM32WIhHGCmYL/MOMd4V8TobJ4F1UoLLEmAee1mtcC1HDJiqS1lUBT9YYQmXtOtxz6DGnCeAMZKHlLYamGkQ57TEKDjx4mMjdIcrxMaFS3uq2O8roa+d2DKBNsRyFI91/rr9KBF2zvZK0YId2iMzhwtuP6QJlE8YzKnhEXQPAPVlgav49f/wmX/3IVbdh4KtQGxClUbyhepZliE8hscOAO1s96n6rmqfEuXRUTVChBq423/V3tJFCKVAM7+ZZk4w1628CUvDz7dqswIJPr3V9tzA8ujwLkvFy0xIFnbkZa6xv+OTz124vfJgClQini+AkykpIcLYpY";
        var encryptedData = "5db4nru87HWpj6feF/EzSpetRtjaSQvXcFFQnV8ECjNCPXG4quHcEIqGxSEnWiVz3+sj9qY1wu/Sdq4fXCU+iBUK3Davm/0FaLrI5FgDzNgIYf1CHIMrxln++kISoqEEkyTPWiKSfxR0zMVKBdEgSIrOXbrN0Q41aWW4uCMqyFkg2vHzYRO6vd4eIQjGdANaKjA1CCGm6bIaTRyPrFINGmyN+jW/A+yUkjcgCO0g0RfEiFzhcSThiRfyNSPPeySm7KoTNz/+dV1PvPFZLTLhtqrRnw2d9oQmLhNlJkY/5msR6BwoyQol2Ek3sw4xVgIx9Bf6y8GxEHNWzBMK+scIIJDG+20ObC+Doe57+p3adn8f6UOlLRW0HI6SH9IAhemWWZzZhqDG7RVZLKyoKPTr7TEUDWrlr6hBdCJc8uOfUjfGP7KH+lBuXQJEouyQxLoyr4JXscWBLnrb621WnxZ2B4xXY2HaHqCwIzQmMLxmD3LuoWmKx5Sv4jl/YbNDKnIp";
//        var iv = "InynLYBAI6D0n2hap8vgEA==";
        var iv = "U4oUy+9e4bgVhncO/hxbXg==";
        System.out.println(new AES.CBC(Base64Kit.decode(sessionKey), Base64Kit.decode(iv))
                .decrypt(encryptedData));

    }

}
